package pcircles.android.study;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
    private ListView mListAppInfo;
	public static final String APP_NAME = "com.android.applicationViewer";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set layout for the main screen
        setContentView(R.layout.layout_main);        
        // load list application
        mListAppInfo = (ListView)findViewById(R.id.lvApps);
        // create new adapter
        AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
        // set adapter to list view
        mListAppInfo.setAdapter(adapter);
        // implement event when an item on list view is selected
        mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
        	@Override
      public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        		Context context = parent.getContext();
        		// get the list adapter
        		AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
        		// get selected item on the list
        		ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
        		// launch the selected application
        		Intent intent = new Intent(context,AppPrivacySettingActivity.class);
        		 
        		//se lo mando al intent
        		intent.putExtra(APP_NAME, appInfo.packageName);
        		
        		//averiguo quien es la activity padre y abro ventana de permisos
        		Activity activity = (Activity)context;
        		activity.startActivity(intent);
        		
        	}
		});
    }
}
